package com.qfedu.yanglao20admin.article.service.Impl;

import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.article.Article;
import com.qfedu.yanglao20admin.article.dao.ArticleTypeDao;
import com.qfedu.yanglao20admin.article.service.ArticleTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {


    @Resource
    private ArticleTypeDao articleTypeDao;

//    @Resource
//    private ArticleDao articleDao;


    /**
     * 查询所有文章类型
     *
     * @return
     */
    @Override
    public List<Article> ListArticleType() {
        return articleTypeDao.selectListAll();
    }

    /**
     * 删除文章类型
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        articleTypeDao.deleteById(id);
    }

    /**
     * 添加文章类型
     *
     * @param article
     */
    @Override
    public void addArticleType(Article article) {
        if (article.getArticleType() == null || article.getArticleType().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请写文章类型名称");
        }
        Article articleTitle = articleTypeDao.selectByArticleTitle(article.getArticleTitle());
        if (articleTitle != null) {
            throw new CommonException(CommonConstants.ERROR, "该文章类型已存在");
        }

        articleTypeDao.insertArticleType(article);
    }

    /**
     * 跟类型id 查询类型信息
     *
     * @param id
     * @return
     */
    @Override
    public Article queryArticleType(Integer id) {
        return articleTypeDao.selectArticleById(id);
    }

    /**
     * 更新文章类型
     *
     * @param article
     */
    @Override
    public void updateArticleType(Article article) {
        if (article.getArticleType() == null || article.getArticleType().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请写文章类型名称");
        }
        
        articleTypeDao.updateArticle(article);
    }

    /**
     * 查询文章内容 根据id
     *
     * @param id
     * @return
     */
    @Override
    public Article selectByArticleId(Integer id) {
        return articleTypeDao.selectArticleById(id);
    }

    /**
     * 根据文章类型获取类型id 添加下级
     *
     * @param article
     * @return
     */
    @Override
    public void addIdByName(Article article) {
        //通过前端发送的文章id查询出来对应的文章类型id
        Article article1 = articleTypeDao.selectArticleId(article.getId());
        Integer typeId = article1.getTypeId();

        String articleType = article.getArticleType();
        Integer articleSort = article.getArticleSort();
        Integer articleInfo = article.getArticleInfo();

        //把相对应的数据()装到一个新实体类对象中
        Article article2 = new Article();
        article2.setTypeId(typeId);
        article2.setArticleType(articleType);
        article2.setArticleSort(articleSort);
        article2.setArticleInfo(articleInfo);

        articleTypeDao.insertArticleGrade(article2);
    }
}
